#!/bin/bash
##############################################################################
# Copyright (c) 2016-20, Lawrence Livermore National Security, LLC and Umpire
# project contributors. See the COPYRIGHT file for details.
#
# SPDX-License-Identifier: (MIT)
##############################################################################
replay_tests_dir=$1
tools_dir=$2
testprogram=$replay_tests_dir/replay_tests
diffprogram=$tools_dir/replaydiff
replayprogram=$tools_dir/replay
topdir=$tools_dir/..

function cleanupandexit {
  mydir=`pwd`
  cd $topdir
  for f in `find . -name '*.replaybin' ; find -name '*.replay'`;
  do
    rm -f $f
  done
  cd $mydir
  exit $1
}

#
# The following program will generate a file of Umpire activity that
# will be replayed.
#
echo UMPIRE_REPLAY="On" $testprogram
UMPIRE_REPLAY="On" $testprogram
if [ $? -ne 0 ]; then
    echo "Failed: Unable to run $testprogram"
    cleanupandexit 1
fi

echo $diffprogram $replay_tests_dir/test_output.good umpire.*.0.replay
$diffprogram $replay_tests_dir/test_output.good umpire.*.0.replay
if [ $? -ne 0 ]; then
    echo "Diff failed on file generated by test"
    cleanupandexit 1
fi

/bin/mv umpire*replay replay.replay
echo UMPIRE_REPLAY="On" $replayprogram -i replay.replay
UMPIRE_REPLAY="On" $replayprogram -i replay.replay
if [ $? -ne 0 ]; then
    echo "$replayprogram Failed"
    cleanupandexit 1
fi

echo $diffprogram $replay_tests_dir/test_output.good umpire.*.0.replay
$diffprogram $replay_tests_dir/test_output.good umpire.*.0.replay
if [ $? -ne 0 ]; then
    echo "Diff failed on file generated by replay tool"
    cleanupandexit 1
fi

cleanupandexit 0
